package com.core.utils;

import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
@Slf4j
public class DateUtil {


    /**
     * 默认日期格式yyyy-MM-dd
     */
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

    public static String format(Date date, String format) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

    public static String format(LocalDate date, String format) {
        if (date == null) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return date.format(formatter);
    }

    public static String format(LocalDateTime date, String format) {
        if (date == null) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return date.format(formatter);
    }

    public static Date convert(String dateStr, String formatStr) {
        if (Strings.isNullOrEmpty(dateStr)) {
            return null;
        }
        DateFormat sdf = new SimpleDateFormat(formatStr);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            throw new RuntimeException("日期转换错误 date:" + dateStr + "format:" + formatStr);
        }
    }

    public static LocalDateTime toLocalDateTime(String dateStr, String formatStr) {
        if (Strings.isNullOrEmpty(dateStr)) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatStr);
        return LocalDateTime.parse(dateStr, formatter);
    }

    public static LocalDateTime toLocalDateTime(Date date) {
        if (date == null) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static LocalDate toLocalDate(Date date) {
        if (date == null) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static Date toDate(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            return null;
        }
        Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    public static Date toDate(LocalDate localDate) {
        if (localDate == null) {
            return null;
        }
        Instant instant = LocalDateTime.of(localDate, LocalTime.MIN).atZone(ZoneId.systemDefault()).toInstant();
        return Date.from(instant);
    }

    public static Date getStartOfDay(Date date) {
        if (date == null) {
            return null;
        }
        LocalDate localDate = toLocalDate(date);
        return toDate(LocalDateTime.of(localDate, LocalTime.MIN));
    }

    public static Date getEndOfDay(Date date) {
        if (date == null) {
            return null;
        }
        LocalDate localDate = toLocalDate(date);
        return toDate(LocalDateTime.of(localDate, LocalTime.MAX));
    }

    public static int compare_date(Date date1, Date date2) {
        return compare_date2(DateUtil.dateToString(date1, DateUtil.DEFAULT_DATE_FORMAT), DateUtil.dateToString(date2, DateUtil.DEFAULT_DATE_FORMAT));
    }

    public static int compare_date2(String DATE1, String DATE2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (ParseException e) {
            log.error(e.getMessage(), e);
        }
        return 0;
    }

    /**
     * 将date按照指定格式转换为字符串
     *
     * @param date
     * @return
     */
    public static String dateToString(Date date) {
        if (date == null) {
            return "";
        }
        DateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
        return sdf.format(date);
    }

    /**
     * 将date按照指定格式转换为字符串
     *
     * @param date
     * @param formatStr
     * @return
     */
    public static String dateToString(Date date, String formatStr) {
        DateFormat sdf = new SimpleDateFormat(formatStr);
        return sdf.format(date);
    }

}
